﻿



var CalculatorStation = function (totalPriceStationElement, scheduleElement, flifgtArrivalsElement, concernElement, tourIdElement, ticketIdElement, priceAdultElement, priceChildElement, dateElement, vatStationElement, seasonElement, quantityAdultElement, quantityChildElement) {

    //ham tinh vat Station
    var fncPriceVatStation = function () {
        var ticketId = $(ticketIdElement).val();
        var priceAdults = $(priceAdultElement).val();
        var priceChild = $(priceChildElement).val();
        var quantityAdults = $(quantityAdultElement).val();
        var quantityChild = $(quantityChildElement).val();
        var vatStation = $(vatStationElement).val();
        var priceStation = 0;
        var priceVatStation = 0;
        if (ticketId > 0) {
            priceStation = (priceAdults * quantityAdults) + (priceChild * quantityChild);
            priceVatStation = priceStation;
            if (vatStation > 0 && vatStation <= 100) {
                priceVatStation = priceStation + priceStation * (vatStation / 100);
            }
        }
        $(totalPriceStationElement).val(priceVatStation);
    };
    //ham tinh totalpriceStation
    $(ticketIdElement).change(function () {
        var ticketId = $(this).val();
       
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "PriceTicKetAri", "TicketId": ticketId },
            dataType: "json",
            success: function (data) {
                if (data && data.isError == false) {
                    $(priceAdultElement).val(data.PriceAdult);
                    $(priceChildElement).val(data.PriceChild);
                    fncPriceVatStation();
                }

            }
        });

    });

    //ham load price Station
    var fncLoadTicketStation = function (ticketId) {
        var typeId = 2;
        var scheduleId = $(scheduleElement).val();
        var date = $(dateElement).val();
        var flightArrival = $(flifgtArrivalsElement).val();
        var concernId = $(concernElement).val();
        var season = seasonElement;
        if (scheduleId != -1 && flightArrival != -1 && concernId != -1 && date != NaN) {
            $.ajax({
                type: "POST",
                url: "/ajax.html",
                data: { "function": "PriceListBy", "TypeId": typeId, "ScheduleId": scheduleId, "Date": date, "FlightArrival": flightArrival, "ConcernId": concernId, "Season": season },
                dataType: "json",
                success: function (data) {

                    $(ticketIdElement).html('<option value="-1">--- Chọn vé ---</option>');


                    if (data && data.isError == false) {
                        $.each(data.array, function (index, item) {
                            $(ticketIdElement)
                                .append($("<option></option>")
                                    .attr("value", item.ID)
                                    .text(item.Name));
                        });
                        if (ticketId > 0) { $(ticketIdElement).val(ticketId); }

                    }
                }
            });
        }

    };

    //thay doi VAT
    $(vatStationElement).keyup(fncPriceVatStation);
    $(scheduleElement).change(fncLoadTicketStation);
    $(dateElement).change(fncLoadTicketStation);
    $(flifgtArrivalsElement).change(fncLoadTicketStation);
    $(concernElement).change(fncLoadTicketStation);

    $('#btnInsertStation').live("click", function () {
        var quantityAdult = $(quantityAdultElement).val();
        var quantityChild = $(quantityChildElement).val();
        var typeId = 2;
        var tourId = parseInt(tourIdElement);
        var ticketId = $(ticketIdElement).val();
        var priceAdult = $(priceAdultElement).val();
        var priceChild = $(priceChildElement).val();
        var date = $(dateElement).val();
        var vatStation = $(vatStationElement).val();
        var calculatorId = $(this).attr("calculatorid");
        var className = $(this).attr("class");
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "InsertStation", "TypeId": typeId, "TourId": tourId, "TicketId": ticketId, "PriceAdult": priceAdult, "PriceChild": priceChild, "Date": date, "QuantityAdult": quantityAdult, "QuantityChild": quantityChild, "CalculatorId": calculatorId, "ClassName": className, 'VatStation': vatStation },
            dataType: "json",
            success: function (data) {

                $(".ListStation").remove();
                $(scheduleElement).val('-1');
                $(flifgtArrivalsElement).val('-1');
                $(concernElement).val('-1');
                $(dateElement).val('');
                $(ticketIdElement + ' > option').remove();
                $(priceAdultElement).val('');
                $(priceChildElement).val('');
                $(quantityAdultElement).val('');
                $(quantityChildElement).val('');
                $(vatStationElement).val('');
                $(totalPriceStationElement).val('');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $('#new-ticket-station')
                            .before($("<tr class='ListStation'></tr>").attr("id", 'ticket-station-' + item.CalculatorId).html('<td></td>' +
                                '<td >' + item.ScheduleName + '</td>' +
                                '<td >' + item.DateBegin + '</td>' +
                                '<td >' + item.FlightArrivals + '</td>' +
                                '<td >' + item.ConcernFullName + '</td>' +
                                '<td >' + item.TicketName + '</td>' +
                                '<td >' + item.PriceAdult + '</td>' +
                                '<td >' + item.PriceChild + '</td>' +
                                '<td >' + item.QuantityAdult + '</td>' +
                                '<td >' + item.QuantityChild + '</td>' +
                                '<td>' + item.VatStation + '</td>' +
                                '<td >' + item.TotalPriceStation + '</td>' +
                                
                                '<td></td>' +
                                '<td>' +
                                '<input class="btnUpdateStation"  type="button" value="Cập nhật" calculatorid="' + item.CalculatorId + '"   />' +
                                '<input class="btnDeleteStation"  type="button" value="Xóa" calculatorid="' + item.CalculatorId + '"   />' +
                                '</td>'));

                    });
                    $('#TotalTicketStationPrices').html(data.TotalTicketStationPrices);
                    $('#TotalTicketStationPricesAdults').html(data.TotalTicketStationPricesAdults);
                    $('#TotalTicketStationPricesChild').html(data.TotalTicketStationPricesChild);

                    $('#btnInsertStation').attr("calculatorid", "-1").val("Thêm");
                    $('.btnUpdateStation').show();
                    $('#btnDeleteStation').hide();
                }

            }
        });
    });

    $('.btnUpdateStation').live("click", function () {
        var calculatorId = $(this).attr("calculatorid");
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "UpdateStation", "CalculatorId": calculatorId },
            dataType: "json",
            success: function (data) {
                $('#ticket-station-' + calculatorId).remove();
                $(scheduleElement).val(data.ScheduleId);
                $(dateElement).val(data.DateBegin);
                $(flifgtArrivalsElement).val(data.FlightArrivals);
                $(concernElement).val(data.ConcernId);
                fncLoadTicketStation(data.TicketId);
                $(priceAdultElement).val(data.PriceAdult);
                $(priceChildElement).val(data.PriceChild);
                $(quantityAdultElement).val(data.QuantityAdult);
                $(quantityChildElement).val(data.QuantityChild);
                $(vatStationElement).val(data.VatStation);
                $(totalPriceStationElement).val(data.TotalPriceStation);
                $('#btnInsertStation').attr("calculatorid", calculatorId).val("Lưu");
                $('.btnUpdateStation').hide();
                $('#btnDeleteStation').show();
            }
        });


    });

    $('.btnDeleteStation').live("click", function () {

        var quantityAdult = $(quantityAdultElement).val();
        var quantityChild = $(quantityChildElement).val();
        var typeId = 2;
        var tourId = parseInt(tourIdElement);
        var ticketId = $(ticketIdElement).val();
        var priceAdult = $(priceAdultElement).val();
        var priceChild = $(priceChildElement).val();
        var date = $(dateElement).val();
        var vatStation = $(vatStationElement).val();
        var calculatorId = $(this).attr("calculatorid");
        var className = $(this).attr("class");
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "InsertStation", "TypeId": typeId, "TourId": tourId, "TicketId": ticketId, "PriceAdult": priceAdult, "PriceChild": priceChild, "Date": date, "QuantityAdult": quantityAdult, "QuantityChild": quantityChild, "CalculatorId": calculatorId, "ClassName": className, 'VatStation': vatStation },
            dataType: "json",
            success: function (data) {

                $(".ListStation").remove();
                $(scheduleElement).val('-1');
                $(flifgtArrivalsElement).val('-1');
                $(concernElement).val('-1');
                $(dateElement).val('');
                $(ticketIdElement + ' > option').remove();
                $(priceAdultElement).val('');
                $(priceChildElement).val('');
                $(quantityAdultElement).val('');
                $(quantityChildElement).val('');
                $(vatStationElement).val('');
                $(totalPriceStationElement).val('');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $('#new-ticket-station')
                            .before($("<tr class='ListStation'></tr>").attr("id", 'ticket-station-' + item.CalculatorId).html('<td></td>' +
                                '<td >' + item.ScheduleName + '</td>' +
                                '<td >' + item.DateBegin + '</td>' +
                                '<td >' + item.FlightArrivals + '</td>' +
                                '<td >' + item.ConcernFullName + '</td>' +
                                '<td >' + item.TicketName + '</td>' +
                                '<td >' + item.PriceAdult + '</td>' +
                                '<td >' + item.PriceChild + '</td>' +
                                '<td >' + item.QuantityAdult + '</td>' +
                                '<td >' + item.QuantityChild + '</td>' +
                                '<td>' + item.VatStation + '</td>' +
                                '<td >' + item.TotalPriceStation + '</td>' +
                                
                                '<td></td>' +
                                '<td>' +
                                '<input class="btnUpdateStation"  type="button" value="Cập nhật" calculatorid="' + item.CalculatorId + '"   />' +
                                '<input class="btnDeleteStation"  type="button" value="Xóa" calculatorid="' + item.CalculatorId + '"   />' +
                                '</td>'));

                    });
                    $('#TotalTicketStationPrices').html(data.TotalTicketStationPrices);
                    $('#TotalTicketStationPricesAdults').html(data.TotalTicketStationPricesAdults);
                    $('#TotalTicketStationPricesChild').html(data.TotalTicketStationPricesChild);

                    $('#btnInsertStation').attr("calculatorid", "-1").val("Thêm");
                    $('.btnUpdateStation').show();
                    $('#btnDeleteStation').hide();
                }

            }
        });
    });
};
